<?php
namespace app\admin\controller;
use think\Db;
use app\admin\controller\Admin;
use think\Controller;
use think\Request;

class Role extends Admin
{	
	

	public function __construct(){
		parent::initialize();
		// var_dump($this->initialize()) ; 

	}

    //显示角色页面
    public function index()
    {


        $list = Db::name('tb_role')->field(['id', 'role'])->select();
            	//获取当前的模块
        return $this->fetch('role/role',[
            'list' => $list,
        ]);

        //获取模块下的控制器

    }

    //显示添加角色页面
    public function addshow()

    {
        $list = Db::name('tb_authorization')->field(['id', 'authorization'])->select();
    	
    	return $this->fetch('role/add',[
                    'list'=>$list,
                ]);
    }

    public function add(Request $request)
    {   
        $data = Request::instance()->post();
     
        if (!empty($data['name'])) {
                 $list = Db::name('tb_role')->insertGetid(['role'=>$data['name']]);
        }else{
            return $this->error('角色名不能为空');
        }
//         var_dump($data);
         if ($list) {
             foreach ($data as $k => $v) {

                 if ($k!='name') {
                      $arr = Db::name('tb_role_authorization')->data(['rid'=>$list,'aid'=>$v])->insert();
                 }
             }      
              if ($arr) {
                 return   $this->success('添加角色成功','index');
              } else {
                return  $this->sucess('角色已添加,未分配权限','index');
              }               
                                     
        }                            

    }



    //显示编辑页面
    public function edit($id)
    {   
        
        $res = Db::name('tb_role')->where('id',$id)->field(['id', 'role'])->select();
//        var_dump($res);
        $data = Db::name('tb_role_authorization')->where('rid',$id)->field(['aid'])->select();
        foreach ($data as $k => $v) {
           $dat[] = $v['aid'] ;
        }

        if ($data==null) {
            $dat[]=0;
        }
        
        $list = Db::name('tb_authorization')->field(['id', 'authorization'])->select();
        return $this->fetch('role/edit',[
                    'list'=>$list,
                    'res'=>$res,
                    'dat'=>$dat,
            ]);

    }

    //编辑页面
    public function update(Request $request)
    {
        $data = Request::instance()->post();
        
        $list = Db::name('tb_role_authorization')->where('rid',$data['rid'])->delete();
        var_dump($data);
        foreach ($data as $k => $v) {

            if ($k!='name'&&$k!='rid') {
                 $list = Db::name('tb_role_authorization')->data(['rid'=>$data['rid'],'aid'=>$v])->insert();                
            }
        }

        if ($list) {
           return $this->success('更改成功','index');
        }

    }


    public function delete(Request $request)
    {
        $data = Request::instance()->post();
        $res = Db::name('tb_role')->where('id',$data['id'])->delete();
        if ($res) {
            
            $list = Db::name('tb_role_authorization')->where('rid',$data['id'])->delete();
        }

        if ($list) {
            return $list;
          
        }

    }

}